<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 达达 <ccbox_net@163.com>
// +----------------------------------------------------------------------
namespace api\goods\controller;

use cmf\controller\RestAdminBaseController;

use api\goods\service\BrandService;

class AdminBrandController extends RestAdminBaseController
{

    /**
     * 品牌列表
     * @throws \think\db\exception\DbException
     * @OA\Get(
     *     tags={"goods"},
     *     path="/admin/goods/brand",
     *     summary="品牌列表",
     *     description="品牌列表",
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "success","data":{
     *              "list":{
     *                  {}
     *              },
     *              "total":1
     *          }})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function index()
    {

        $param = $this->request->param([
            'limit' => 5,
            'status' => 'yes',
            'list_order_by' => 'list_order asc',
            'keyword' => '',
            'id_start' => null,
            'id_end' => null
        ]);

        $service = new BrandService();
        $list = $service->getSearchList($param, ['*'], $param['list_order_by']);

        $this->success('success', ['list' => $list->items(), 'total' => $list->total()]);
    }

    /**
     * 添加品牌
     * @throws \think\db\exception\DbException
     * @OA\Post(
     *     tags={"goods"},
     *     path="/admin/goods/brand",
     *     summary="添加品牌",
     *     description="添加品牌",
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "success","data":{
     *              "item":{}
     *          }})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();

            $result = $this->validate($data['form'], 'AdminBrand');
            if ($result !== true) {
                $this->error($result);
            }

            $service = new BrandService();
            $dataModel = $service->save($data['form']);

            $this->success(lang('ADD_SUCCESS'), ['item' => $dataModel]);
        }
    }

    /**
     * 编辑品牌
     * @throws \think\db\exception\DbException
     * @OA\Put(
     *     tags={"goods"},
     *     path="/admin/goods/brand/{id}",
     *     summary="编辑品牌",
     *     description="编辑品牌",
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="品牌id",
     *         required=true,
     *         @OA\Schema(
     *             type="integer",
     *         )
     *     ),
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminLinkSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminLinkSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "保存成功","data":""})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function update($id)
    {
        if ($this->request->isPut()) {
            $data = $this->request->param();

            $result = $this->validate($data['form'], 'AdminBrand');
            if ($result !== true) {
                $this->error($result);
            }

            $service = new BrandService();
            $dataModel = $service->save($data['form'], $id);

            $this->success(lang('EDIT_SUCCESS'), ['formdata'=>$data,'item' => $dataModel]);
        }
    }

    /**
     * @OA\Delete(
     *     tags={"goods"},
     *     path="/admin/goods/brand/{id}",
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="brand id",
     *         required=true,
     *         @OA\Schema(
     *             type="string",
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "删除成功!","data":""})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error","data":""})
     *     ),
     * )
     */

    public function delete()
    {
        $param = $this->request->param();

        $id = $this->request->param('id');
        if (isset($param['id'])) {
            $ids = [$id];
        } else {
            $ids = $this->request->param('ids/a');
        }

        // $setStatus = isset($param["yes"]) ? 1 : 0;

        $service = new BrandService();
        $res = $service->setStatus($ids, 0, 'status');
        $this->success('删除成功！删除数量：' . $res, '');
    }

    /**
     * 品牌发布
     * @throws \think\db\exception\DbException
     * @OA\Post(
     *     tags={"goods"},
     *     path="/admin/goods/brand/status",
     *     summary="品牌发布",
     *     description="品牌发布",
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "success","data":{
     *              "item":{}
     *          }})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function publish()
    {
        $ids = $this->request->param('ids/a');

        $param = $this->request->param();
        // $setStatus = isset($param["yes"]) ? 1 : 0;
        $setStatus = $param['status'];

        $service = new BrandService();
        $res = $service->setStatus($ids, $setStatus, 'status');

        if ($res) {
            $resMsg = $setStatus ? '发布成功！' : '取消发布成功！';
            $this->success($resMsg, '');
        }
    }

    /**
     * 品牌排序
     * @throws \think\db\exception\DbException
     * @OA\Post(
     *     tags={"goods"},
     *     path="/admin/goods/brand/list/order",
     *     summary="品牌排序",
     *     description="品牌排序",
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminGoodsBrandSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "success","data":{
     *              "item":{}
     *          }})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function listOrder()
    {
        parent::listOrders('goods_brand');
        $this->success("排序更新成功！", '');
    }

}